//
// Created by DengLibin on 2018/12/12 0012.
//
/**
 * 数据类型转换
 */
#include <iostream>
#include <string>
using namespace std;

//new高级用法
const int buf = 512;
int N(5);//数组的长度
char buffer[buf];//在静态区
void advanceNew()
{
    double *p1, *p2;
    p1 = new double[N];
    p2 = new (buffer)double[N];//在指定区域分配内存
    for (int i = 0; i < N; i++)
    {
        p1[i] = p2[i] = i + 23.3;
        std::cout << &p1[i] << "->" << p1[i] << "\t";
        std::cout << &p2[i] << "->" << p2[i] << endl;
    }
    delete p1;
}
//类型转换
//4种类型转换方式
void castType()
{
    float f = 2.01;
    //1、static_cast 常用
    int i = static_cast<int>(f);
    cout << i << endl;

    //2、const_cast 只能用于指针类型,去掉const属性
    int c = 8;
    const int *p = &c; //指针指向的内存空间不能修改
    int *p2 = const_cast<int*>(p);
    *p2 = 5;
    cout << "c = " << c << endl;
    //3、dynamic_cast 对象向下转型(继承关系)

    //4、reinterpret_cast 用于指针(转换指针的类型)
    char ch = 'd';
    char *p_ch = &ch;
    int * p_ch1 = reinterpret_cast<int*>(p_ch);
    cout << *p_ch1 << endl;
}

int main14()
{
    //advanceNew();
    castType();
    return 0;
}

